home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.02 Feb 91 / Zempel Letter / DrawMyStuff.c
Encoding:
C/C++ Source or Header  |  1990-10-29  |  1.4 KB  |  62 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Source  - DrawMyStuff.c
  3.  * based on Mark B. Kauffman's article
  4.  * in the 9/90 MacTutor
  5.  *
  6.  * revised to demonstrate polymorphism
  7.  * -- the way a subclass can override
  8.  * its ancestor’s methods  
  9.  */
  10.  
  11. #include "oops.h"
  12. #include "CShape.h"
  13.  
  14. void DrawMyStuff()
  15. {
  16.    int            i;
  17.     
  18.    /* set up a big generic array    */
  19.    CShape        *myShape[9];
  20.    
  21.    /* let’s create 4 rectangles...    */
  22.    for (i=0; i<4; i++)
  23.        myShape[i] = new(CRectangle);
  24.    /* ... one line...             */
  25.    myShape[4] = new(CLine);
  26.    /* ... and 4 ovals         */
  27.    for (i=5; i<9; i++)
  28.        myShape[i] = new(COval);
  29.  
  30.    /* place the rectangles     */
  31.    myShape[0]->SetShapeLoc(200,100,300,200);
  32.    myShape[1]->SetShapeLoc(210,110,290,190);
  33.    myShape[2]->SetShapeLoc(220,120,280,180);
  34.    myShape[3]->SetShapeLoc(230,130,270,170);
  35.  
  36.    /* place the line         */
  37.    myShape[4]->SetShapeLoc(100,50,300,50);
  38.  
  39.    /* place the ovals         */
  40.    myShape[5]->SetShapeLoc(100,100,200,200);
  41.    myShape[6]->SetShapeLoc(110,110,190,190);
  42.    myShape[7]->SetShapeLoc(120,120,180,185);
  43.    myShape[8]->SetShapeLoc(130,130,170,170);
  44.  
  45.    /* here's the fun part! send each shape */
  46.    /* a Draw() message, generically         */
  47.    for (i=0; i<9; i++)
  48.        myShape[i]->Draw();
  49.        
  50.    /* relocate the line and Erase() */
  51.    myShape[4]->SetShapeLoc(210,110,290,110);
  52.    myShape[4]->Erase();
  53.        
  54.    /* delete everything, again generically */
  55.    for (i=0; i<9; i++)
  56.    {
  57.        delete(myShape[i]);
  58.    }
  59. }
  60.  
  61. /* end of listing - DrawMyStuff.c */
  62.